//
//  BaseNavigationController.swift
//
//  Created by ink on 2024/6/21.
//  Copyright © 2024 ZM. All rights reserved.
//

import UIKit

open class BaseNavigationController: UINavigationController {

    public override func viewDidLoad() {
        super.viewDidLoad()

        self.delegate = self
    }
    
    public override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        ///隐藏tabbar
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        } else {
            viewController.hidesBottomBarWhenPushed = false
        }
        ///打开右滑返回
        interactivePopGestureRecognizer?.isEnabled = true
        interactivePopGestureRecognizer?.delegate = nil
        super.pushViewController(viewController, animated: animated)
    }
    
    public override var shouldAutorotate: Bool { visibleViewController?.shouldAutorotate ?? false }
    
    public override var supportedInterfaceOrientations: UIInterfaceOrientationMask { visibleViewController?.supportedInterfaceOrientations ?? .portrait }
    
    public override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation { visibleViewController?.preferredInterfaceOrientationForPresentation ?? .portrait }
    public override var childForStatusBarHidden: UIViewController? { topViewController }
    deinit {
        NotificationCenter.default.removeObserver(self)
    }
}

extension BaseNavigationController: UINavigationControllerDelegate {
    public func navigationController(_ navigationController: UINavigationController, didShow viewController: UIViewController, animated: Bool) {
        if viewControllers.count <= 1 {
            interactivePopGestureRecognizer?.isEnabled = false
        }
    }
}
